﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Data.OleDb;
using Csla;
using Csla.Data;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace BO
{

    public enum dbType { SqlServer, OleDb }
    public static class DbFactory
    {

        public static dbType DbType
        {
            get
            {
              //  var str = ConfigurationManager.ConnectionStrings["conn"].ProviderName;
                var str = "";
                switch (str)
                {

                    case "System.Data.OleDbClient":
                        return dbType.OleDb;
                    case "System.Data.SqlClient":
                    default:
                        return dbType.SqlServer;

                }
            }
        }
        public static DbParameter AddWithValue(this DbParameterCollection dbp, string parameterName, object value)
        {

            DbParameter p = null;

            switch (DbType)
            {
                case dbType.OleDb:
                    p = new OleDbParameter(GlobalVar.Prefix + parameterName, value);
                    break;
                default:
                case dbType.SqlServer:
                    p = new SqlParameter(GlobalVar.Prefix+ parameterName, value);
                    break;
            }
            dbp.Add(p);
            return p;
        }

        public static DbConnection GetConnection(string pConnString)
        {

            switch (DbType)
            {
                case dbType.OleDb:
                    return new OleDbConnection(pConnString);
                default:
                case dbType.SqlServer:
                    return new SqlConnection(pConnString);

            }

        }

        public static int ExecuteNonQuery(DbCommand cm)
        {
            ChangeCommand(cm);
            return cm.ExecuteNonQuery();
        }

        public static DbDataReader ExecuteReader(DbCommand cm)
        {
            try
            {

                ChangeCommand(cm);
                if (cm.Connection.State != ConnectionState.Open)
                    cm.Connection.Open();
                return cm.ExecuteReader();

            }
            catch (ExceptionEx exp)
            {
                throw (exp);
            }
        }

        public static DbDataAdapter GetDataAdapter(DbCommand cm)
        {
            if (DbType == dbType.SqlServer)
                return new SqlDataAdapter((SqlCommand)cm);
            else
                return new OleDbDataAdapter((OleDbCommand)cm);
        }
        public static DbParameter GetDbParameter(string parameterName, object parameterValue)
        {
            if (DbType == dbType.SqlServer)
                return new SqlParameter(parameterName, parameterValue);
            else
                return new OleDbParameter(parameterName, parameterValue);
        }
        static void ChangeCommand(DbCommand cm)
        {
            if (DbFactory.DbType == dbType.OleDb)
            {

                foreach (DbParameter item in cm.Parameters)
                {
                    var str = item.ParameterName;
                    item.ParameterName = str;
                }
                var str1 = cm.CommandText.Replace("@", ":");
                cm.CommandText = str1;

            }
        }



        public static IsolationLevel IsolationLevel { 
            get { return System.Data.IsolationLevel.Serializable; } }
    }


}
